package com.glut.gultguy.controller;


import com.glut.gultguy.To.goodsTo;
import com.glut.gultguy.entity.Goods;
import com.glut.gultguy.mapper.GoodsMapper;
import com.glut.gultguy.service.GoodsService;
import com.glut.gultguy.vo.goodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired(required = false)
    private GoodsMapper mapper;
    @Autowired
    private GoodsService service;

    @GetMapping("/list")
    @ResponseBody
    public List<goodsVo> getGoods(){
        return service.getList();
    }

    @GetMapping("/click/{id}")
    @ResponseBody
    public int addGoodsClick(@PathVariable("id") String goodsID){
        return service.addClick(goodsID);
    }

    @PostMapping("/upGoods")
    @ResponseBody
    public Map<String,Integer> upGoods(@RequestBody goodsTo to){
        return service.upGoods(to);
    }

    @PostMapping("/getGood/{id}")
    public goodsVo getGood(@PathVariable("id") String goodId){
        return service.getGood(goodId);
    }
    @PostMapping("/getStatus/{id}")
    public int getStatus(@PathVariable("id") String goodId){
        return this.mapper.getStatus(goodId);
    }
    @PostMapping("/updatePrice/{id}/{price}")
    public int updatePrice(@PathVariable("id") String goodId,@PathVariable("price") Integer price){
        return this.mapper.updatePrice(goodId,price);
    }
    //搜索商品
    @PostMapping("/search/{key}")
    public List<goodsVo> search(@PathVariable("key") String key){
       return  service.search("%"+key+"%");
    }

    //寻找我的发布商品
    @GetMapping("/myGoods/{id}")
    public List<goodsVo> getMyGoods(@PathVariable("id") String publisherId){
        return service.getMyGoods(publisherId);
    }

    //获取交易记录
    @GetMapping("/getDeal/{id}")
    public List<goodsVo> getDeal(@PathVariable("id") String buyerOpenId){
        return service.getDeal(buyerOpenId);
    }
    @PostMapping("/delete{id}")
    public void delete(@PathVariable("id") String id){
        this.service.removeById(id);
    }
}

